package com.controller;
import com.model.*;

import org.json.simple.JSONObject;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

import com.model.ConvictionHistory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
/**
 * Servlet implementation class FQHandler
 */
public class ConvictionController extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ConvictionController() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, 
	 * 							HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, 
							HttpServletResponse response) 
							throws ServletException, IOException {
		FastQuote fq=new FastQuote();
		String userId;
		String conHistId;
		RequestDispatcher rd = null;
		Logger l=Logger.getLogger("FastQuote.class");
		l.info("doget caled successfully");
		JSONObject js = new JSONObject();
		String choice=request.getParameter("choice");
		String msg="";
		
		/*
		 * Responsibility:	To check the user id for registering 
		 * 					the conviction History details. 					
		 */
		
		if(choice.equals("checkuser"))
		{
			String uid=request.getParameter("userId");
			boolean flag=fq.isUserExist(uid);
			if(flag==true)
			{
				js.put("flag","true");
			}
			else
			{
				js.put("flag","false");
			}
			PrintWriter pw=response.getWriter();
			pw.print(js);
			pw.flush();
		}
		
		
		
		
		/*
		 * Responsibility:	To register the conviction History details 
		 * 					corresponding to the user id and store in the 
		 * 					database. 					
		 */
		
		if(choice.equals("registerConviction"))
		{
			String uid=request.getParameter("userId");
			FastQuoteUser u1=new FastQuoteUser(uid);
			String natureOfConviction=
								request.getParameter("natureOfConviction");
			String dateOfConviction=request.getParameter("dateOfConviction");
			String comments=request.getParameter("comments");
			try
			{
				boolean flag=fq.isUserExist(uid);
				if(flag==true)
					{			
						String conId="con_"+com.utility.Utility.getNextVal();
						ConvictionHistory c=
							new ConvictionHistory(conId,
													u1,
													dateOfConviction,
													natureOfConviction,
													comments
												);
						int result=fq.addConviction(c);
						if(result>0)
							{
							l.info("Added conviction details for user " +
									   "with id = "+uid);
								String conId1=fq.displayAddedConvictionId(uid,dateOfConviction);
								msg="Details registered successfully.Conviction history id is :"+conId1;
								request.setAttribute("message", msg);
								rd=request.getRequestDispatcher("Quote_errorDisplay.jsp");
								rd.forward(request, response);
							}
						else
							{
								msg="Conviction History Id is already generated for these details.";
								request.setAttribute("message", msg);
								rd=request.getRequestDispatcher("Quote_errorDisplay.jsp");
								rd.forward(request, response);
							}
					}
				else{
						msg="User does not exist.";
						request.setAttribute("message", msg);
						rd=request.getRequestDispatcher("Quote_errorDisplay.jsp");
						rd.forward(request, response);
					}
			}
			catch (SQLException e1) 
			{
			e1.printStackTrace();
			}
			catch(Exception e1){
				System.out.println(e1);
			}
		}
		/*
		* Responsibility:	To retrieve the Conviction History Details 
		* 					corresponding to the entered userId
		*/
	 if(choice.equalsIgnoreCase("submit1"))
		{
			
			String uid=request.getParameter("userId");
			boolean flag;
			flag = fq.isUserExist(uid);
			if(flag==true)
			{
				
				ArrayList<ConvictionHistory> ac=
										fq.displayConvictionDetails(uid);
				if(ac!=null)
				{
				request.setAttribute("flag", flag);
				request.setAttribute("convictionDetails", ac);
				rd=	request.getRequestDispatcher(
				"ConvictionHistory_Delete3.jsp");
				rd.forward(request, response);
				}
				else
				{
					request.setAttribute("convictionDetails", null);
					rd=	request.getRequestDispatcher(
					"ConvictionHistory_Delete3.jsp");
					rd.forward(request, response);
				}
			}
			else
			{
				request.setAttribute("flag", flag);
				rd=	request.getRequestDispatcher(
				"ConvictionHistory_Delete3.jsp");
				rd.forward(request, response);
			}
			
		}
		/*
		 * Responsibility:	To delete the conviction History details from the
		 * 					database corresponding to the selected conviction
		 * 					id
		 */
		if(choice.equalsIgnoreCase("delete"))
		{
			String[] conId = request.getParameterValues("conId");
			if(conId.length>0)
			{
				int result;
				try {
					result = fq.deleteConvictionHistoryDetails(conId);
					request.setAttribute("result", result);
					if(result >0){
						for(String q : conId){
							l.info("Deleted conviction history details " +
									"with conviction id : "+q);
						}
						request.setAttribute("message", "Conviction details deleted successfully.");
						rd=
						request.getRequestDispatcher(
											"Quote_errorDisplay.jsp");
						rd.forward(request, response);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			else
			{
				request.setAttribute("result",0);
				rd=
				request.getRequestDispatcher("ConvictionHistory_Delete2.jsp");
				rd.forward(request, response);
			}
		}
		
		if(choice.equals("retrieveConvictionID"))
		{
			userId=request.getParameter("user_id");
			boolean flag= fq.isUserExist(userId);
			
			if(flag==true)
			{
				ArrayList<String> conHistIds=fq.retrieveConId(userId);
				if(conHistIds.size()>0)
				{
					js.put("flag","userHasConHist");
					js.put("conHistIds",conHistIds);
				}
				else
				{
					js.put("flag","userHasNoConHist");
					js.put("conHistIds",conHistIds);
				}
				PrintWriter pw=	response.getWriter();
				pw.print(js);
				pw.flush();
			}
			else
			{
				js.put("flag","userDoesNotExist");
				PrintWriter pw=	response.getWriter();
				pw.print(js);
				pw.flush();
			}
		}
		
		if(choice.equals("retrieveConvictionDetails"))
		{
			conHistId=request.getParameter("conHistId");
			String uid=request.getParameter("user_id");	
			if(conHistId.equalsIgnoreCase("all"))
			{
				ArrayList<ConvictionHistory> alConHist=
											fq.retrieveConvictionHistory(uid);
				request.setAttribute("alConHist", alConHist);
			}
			else
			{
				ConvictionHistory conHist=
								fq.retrieveConvictionHistoryOnId(conHistId);
				request.setAttribute("conHist", conHist);
			}
			l.info("selected accident history for user id = "+uid);
			rd=
			request.getRequestDispatcher(
									"ConvictionHistory_RetrieveAllDetails.jsp");
			rd.forward(request, response);
		}

		if(choice.equals("UpdateUserCheck"))
		{
			userId=request.getParameter("user_id");
			boolean flag= fq.isUserExist(userId);
			if(flag==false)
			{
				js.put("flag", "false");
			}
			if(flag==true)
			{
				ArrayList<ConvictionHistory> conHist=
											fq.retrieveConvictionHistory(userId);
				if(conHist.size()>0)
				{
					js.put("flag", "true");
				}
				else
				{
					js.put("flag", "userHasNoConHist");
				}
			}
			PrintWriter pw=	response.getWriter();
			pw.print(js);
			pw.flush();
		}
			
		if(choice.equals("UpdateConvictionHome"))
		{
			userId=request.getParameter("user_id");
			boolean flag= fq.isUserExist(userId);
			if(flag==true)
			{
				request.setAttribute("flag",flag);
				ArrayList<ConvictionHistory> conHist=
										fq.retrieveConvictionHistory(userId);
				if(conHist.size()>0)
				{
					request.setAttribute("conHist", conHist);
					rd=
					request.getRequestDispatcher
											("ConvictionHistory_Update2.jsp");
					rd.forward(request,response);
				}
			}
		}
		
		if( choice.equals("selected_conHistId"))
		{
			conHistId=request.getParameter("conId");
			ConvictionHistory conHist=
								fq.retrieveConvictionHistoryOnId(conHistId);
			request.setAttribute("conHist", conHist);
			rd=request.getRequestDispatcher("ConvictionHistory_Update3.jsp");
			rd.forward(request,response);
		}
		
		if(choice.equals("editConfirm"))
		{
			conHistId=request.getParameter("conHistID");
			String dateOfConviction=request.getParameter("dateOfConviction");
			String natureOfConviction=
									request.getParameter("natureOfConviction");
			String comments=request.getParameter("comments");
			int updateResult=fq.updateConvictionHistory(
														conHistId,
														dateOfConviction,
														natureOfConviction,
														comments
														);

			if(updateResult>0)
			{
				l.info("Updated conviction details " +
						"of conviction history id = "+conHistId);
				request.setAttribute("message", "The details are successfully updated.");
				rd = request.getRequestDispatcher("Quote_errorDisplay.jsp");
				rd.forward(request, response);
				
			}

			else
			{
				request.setAttribute("message", "The details are not successfully updated.");
				rd = request.getRequestDispatcher("Quote_errorDisplay.jsp");
				rd.forward(request, response);
			}
		}
		
		
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, 
	 * 							HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
							HttpServletResponse response) 
							throws ServletException, IOException {
		doGet(request, response);
	}
  }